package com.bookcode.controller;

import com.bookcode.entity.Course;
import com.bookcode.entity.CourseStudent;

import com.bookcode.entity.TeacherCourse;
import com.bookcode.service.*;
//import com.bookcode.service.CourseStudentService;
import com.bookcode.entity.HttpResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class CourseController {

    @Autowired
    CourseService courseService;

    @Autowired
    StudentService studentService;

    @Autowired
    TeacherService teacherService;
    @Autowired
    CourseStudentService courseStudentService;

    @Autowired
    TeacherCourseService teacherCourseService;
    /*
    * 增加课程
    * */
    @PostMapping(value = "/courses")
    public HttpResult add(Course course) {
        HttpResult result = new HttpResult();
        try {

            courseService.save(course);
            result.setMessage("增加成功");
        }catch (Exception e){
            result.setStatus("增加失败");
        }finally {
            return result;
        }
    }

    @GetMapping(value = "/course/{id}")
    public HttpResult queryCourseById(@PathVariable Long id){
        HttpResult<Course> result = new HttpResult<>();
        result.setData(courseService.findCourseById(id));
        return result;
    }

    @GetMapping(value = "/courses")
    public HttpResult queryAll(){
        HttpResult<List<Course>> result = new HttpResult<>();
        result.setData(courseService.findAll());
        result.setStatus("显示所有课程信息：");
        return result;
    }
    //通过课程id删除课程
    @GetMapping(value = "/delete/course/{id}")
    public HttpResult deleteCourseById(@PathVariable String id){
        HttpResult<List<Course>> result = new HttpResult<>();
        try{
            courseService.deleteCourseById(id);
            result.setMessage("删除成功");
        }catch (Exception e){
            result.setMessage("删除失败");
        }finally {
            return result;
        }
    }

    //学生选课
    @PostMapping("/addCourseStudents")
    public HttpResult courseStudent(@ModelAttribute CourseStudent courseStudent)  {
        HttpResult result = new HttpResult();
        try {
            courseStudentService.save(courseStudent);
            result.setStatus(studentService.findStudentByXuehao(courseStudent.getStudent_id()).getName()+"成功选择课程《"+courseService.findCourseByCourseId(courseStudent.getCourse_id()).getName()+"》。");
        }catch (Exception e){
            result.setStatus("该同学已选择本课程！请选择其他课程。");
        }finally {
            return result;
        }
    }
    //老师添加课程
    @PostMapping("/addTeacherCourse")
    public HttpResult teacherCourse(@ModelAttribute TeacherCourse teacherCourse)  {
        HttpResult result = new HttpResult();
        try {
            teacherCourseService.save(teacherCourse);
                result.setStatus(teacherService.findTeacherById(teacherCourse.getTeacher_id()).getName()+ "添加课程<<"+courseService.findCourseById(teacherCourse.getCourse_id()).getName());
        }catch (Exception e){
            result.setStatus("该同学已选择本课程！请选择其他课程。");
        }finally {
            return result;
        }
    }

    /*
     * 增加学生的课程
     * */
    /*@PostMapping(value = "/course/{courseId}/student/{studentId}")
    public HttpResult addCourseStudent(@PathVariable Long courseId,Long studentId) {
        HttpResult result = new HttpResult();
        try {
            Course course = courseService.findCourseById(courseId);
            Student student = studentService.findStudentById(studentId);
            CourseStudent courseStudent = new CourseStudent();
            courseStudent.setCourse(course);
            courseStudent.setStudent(student);
            courseStudentService.save(courseStudent);
            result.setStatus("ok");
        }catch (Exception e){
            result.setStatus("wrong");
        }finally {
            return result;
        }
    }*/
}
